package com.ruoyi;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Map;

import com.ruoyi.accounts.tcp.TcpUsersStruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.context.annotation.EnableAspectJAutoProxy;



/**
 * 启动程序
 *
 * @author 黄字添
 */
@EnableScheduling
@EnableAspectJAutoProxy
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class WWJApplication {
    public static void main(String[] args) {
        // System.setProperty("spring.devtools.restart.enabled", "false");
        SpringApplication.run(WWJApplication.class, args);
        System.out.println("(♥◠‿◠)ﾉﾞ  云平台娃娃机后台启动成功   ლ(´ڡ`ლ)ﾞ  \n" +
                "  __      __  __      __    ____. \n" +
                " /  \\    /  \\/  \\    /  \\  |    | \n" +
                " \\   \\/\\/   /_\\   \\/\\/   /  |    | \n" +
                "  \\        /  \\        /\\__|    | \n" +
                "   \\__/_\\  /    \\__/_\\  /\\________| \n" +
                "        \\/          \\/            \n");
        //createWaitTCPTest();
    }
    
    public static void createWaitTCPTest() {
	    String serverHost = "localhost";
	    int serverPort = 8047;

	    try {
	        Socket socket = new Socket(serverHost, serverPort);
	        System.out.println("Connected to server on port " + serverPort);

	        // 将客户端连接添加到 USERS 映射中
	        String hardwareNumber = "uid12345678"; // 根据实际情况设置硬件编号
	        Map<String, Socket> map = TcpUsersStruct.getUsers();
	        map.put(hardwareNumber, socket);

	        // 创建一个线程来持续接收服务器响应
	        Thread responseThread = new Thread(() -> {
	            try {
	                InputStream inputStream = socket.getInputStream();
	                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
	                String response;
	                while ((response = reader.readLine()) != null) {
	                    System.out.println("Response from server: " + response);
	                }
	            } catch (IOException e) {
	                System.err.println("Error receiving response from server: " + e.getMessage());
	            }
	        });
	        responseThread.start();

	        // 主线程保持连接
	        while (true) {
	            // 在这里可以添加客户端发送请求的逻辑
	            // 例如：通过 socket.getOutputStream() 发送请求给服务器
	            // 这里可以根据需要添加相应的逻辑

	            // 为了示例，这里让主线程休眠一段时间
	            Thread.sleep(1000); // 休眠1秒钟
	        }
	    } catch (IOException | InterruptedException e) {
	        System.err.println("Error communicating with server: " + e.getMessage());
	    }
    }
}
